package blog.service.impl;

import blog.dao.mapper.CategoryMapper;
import blog.dao.pojo.Category;
import blog.service.CategoryService;
import blog.vo.CategoryVo;
import blog.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    // 获取文章专栏
    @Override
    public Result findAll() {
        List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<>());

        return Result.success(copyList(categories));
    }

    public  List<CategoryVo> copyList(List<Category> list){
        ArrayList<CategoryVo> voList = new ArrayList<>();

        list.forEach(
                (o)-> { voList.add(copy(o));}
        );

        return voList;
    }

    public CategoryVo copy(Category category){
        CategoryVo categoryVo = new CategoryVo();
        BeanUtils.copyProperties(category,categoryVo);
        return categoryVo;
    }

    @Override
    public CategoryVo findCategoryById(Long id) {

        Category category = categoryMapper.selectById(id);

        CategoryVo categoryVo = new CategoryVo();

        BeanUtils.copyProperties(category,categoryVo);

        return categoryVo;
    }


}
